package com.atkins.spring.validation;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.atkins.spring.data.IGoonData;

public class GoonValidator implements Validator {

	@Override
	public boolean supports(Class<?> arg0) {
		return IGoonData.GoonDetails.class.isAssignableFrom(arg0);
	}

	@Override
	public void validate(Object obj, Errors err) {
		IGoonData.GoonDetails goon = (IGoonData.GoonDetails) obj;

		ValidationUtils.rejectIfEmptyOrWhitespace(err, "name", "name.empty");
	
		Date now = new Date();
		Calendar cal = new GregorianCalendar();
		cal.setTime(now);
		cal.set(Calendar.YEAR, (cal.get(Calendar.YEAR) - 100));
		if(cal.before(goon.getDob())){
			err.rejectValue("dob", "dob.tooold");
		}
		
	}

}
